using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;

namespace Rogue
{
	/// <summary>
	/// A compass direction.
    /// By Ed Kolis @ http://kolis.dyndns.org/sharproguelib/
	/// </summary>
	public class Direction
	{
		public static readonly Direction North          = new Direction("north", 0, -1);
		public static readonly Direction South          = new Direction("south", 0, 1);
		public static readonly Direction East           = new Direction("east", 1, 0);
		public static readonly Direction West           = new Direction("west", -1, 0);
		public static readonly Direction Northeast      = new Direction("northeast", 1, -1);
		public static readonly Direction Northwest      = new Direction("northwest", -1, -1);
		public static readonly Direction Southeast      = new Direction("southeast", 1, 1);
		public static readonly Direction Southwest      = new Direction("southwest", -1, 1);
		
		private Direction(string name, int dx, int dy)
		{
			m_sName = name;
			m_DX = dx;
			m_DY = dy;
		}

		private Direction(Direction d1, Direction d2)
		{
			m_sName = d1.Name + d2.Name;
			m_DX = d1.DX + d2.DX;
			m_DY = d1.DY + d2.DY;
		}

        // returns null if it's not one of the static 8!
        static public Direction GetDirection(int a_Dx, int a_Dy)
        {
            if (a_Dx == -1)
            {
                if (a_Dy == -1)
                    return Northwest;
                if (a_Dy == 0)
                    return West;
                if (a_Dy == 1)
                    return Southwest;
            }
            if (a_Dx == 0)
            {
                if (a_Dy == -1)
                    return North;
                if (a_Dy == 0)
                    return null;
                if (a_Dy == 1)
                    return South;
            }
            if (a_Dx == 1)
            {
                if (a_Dy == -1)
                    return Northeast;
                if (a_Dy == 0)
                    return East;
                if (a_Dy == 1)
                    return Southeast;
            }

            return null;
        }

		/// <summary>
		/// The name of the direction.
		/// </summary>
        private string m_sName;
        public string Name { get { return m_sName; } }

		/// <summary>
		/// The horizontal change in position.
		/// </summary>
        private int m_DX;
        public int DX { get { return m_DX; } }

		/// <summary>
		/// The vertical change in position.
		/// </summary>
        private int m_DY;
		public int DY { get { return m_DY; } }

        public static Point operator +(Point m_Start, Direction a_Dir)
        {
            return new Point(m_Start.X + a_Dir.m_DX, m_Start.Y + a_Dir.m_DY);
        }

		public override string ToString()
		{
			return Name;
		}
	}
}
